JavaScript

A5.u.elementattr Method

Syntax

A5.u.element.attr(element,attribute,value)

A5.u.element.attr(element,attributeObject)

Arguments

elementelementstring

A pointer to a DOM element or the ID of an element.

attributestringarray

The name(s) of the attribute(s). An array of attribute names can be passed in in order to get, set, check, toggle or remove multiple attributes.

valueany

The value(s) to set or toggle the attribute(s) with.

attributeObjectobject

A object in which the property names are the names of attributes and the values are the values to set or toggle.

Description

Get, set, check, toggle and/or remove attributes of an element.

Discussion

The A5.u.element.attr() method can be used to get, set, check, toggle or remove one or more attributes. If multiple attributes are passed in, then an object will be returned with the object properties aligning with the attribute names, and the values correspondingly. If a single attribute is passed in then the resulting value will be returned directly.

To get the value of an attribute(s), a value of "==" can be used, or the value may be left blank. If value(s) are passed in then the attribute(s) will be set to the new value(s). A null value is passed in to remove the attribute(s). Toggling of a value can be done by adding the prefix "~=" to the value. In this case, if the value of the attribute is the same as the content following the "~=" then the attribute will be removed. Otherwise it will be set to the new value.

A checking operation can be preformed by adding the prefix "?=". In this case the result will be true or false. If the attributes value is equal to the content following the "?=" then true will be return, otherwise a value of false will be returned. A vlaue of "?=*" can be used to check if the attribute has any value.

Example

// assume "id" is the ID of DIV that wants to be styled
var attr = A5.u.element.attr(id,'state','?=*');
// attr = false
attr = A5.u.element.attr(id,['state','message'],['loading','Loading...']);
// attr = {"state": "loading", "message": "Loading..."}
attr = A5.u.element.attr(id,'state','?=*');
// attr = true
attr = A5.u.element.attr(id,'state',null);
// attr = null
attr = A5.u.element.attr(id,'message','Done...');
// attr = "Done..."